﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Autenticacao
{
    public partial class frm_delconta : Form
    {
        int num = 0;
        System.Data.SqlClient.SqlConnection con;

        public frm_delconta()
        {
            InitializeComponent();
        }

        private void cmdcontapesquisa_Click(object sender, EventArgs e)
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
            con.Open();

            try
            {
                SqlCommand command1 = con.CreateCommand();
                command1.CommandText = "SELECT COUNT(*) FROM Utilizador WHERE Login = '" + txtuser.Text + "'";
                num = (Int32)command1.ExecuteScalar();

                if (num == 0)
                {
                    MessageBox.Show("Utilizador não encontrado!", "Insira de novo o nome!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    lblconfirmar.Visible = true;
                    cmdconfirmar.Visible = true;
                    cmdocultar.Visible = true;
                    
                }
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString(), "Erro!");
            }
            con.Close();
        }

        private void cmdconfirmar_Click(object sender, EventArgs e)
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
            con.Open();

            try
            {
                SqlCommand delutil = con.CreateCommand();
                delutil.CommandText = "DELETE FROM Utilizador WHERE Login = '" + txtuser.Text + "'";
                SqlDataReader reader1 = delutil.ExecuteReader();

                MessageBox.Show("Utilizador eliminado com sucesso!", "Eliminar Concluído!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString(), "Erro!");
            }
            con.Close();

            this.Close();
            frm_padmin frm = new frm_padmin();
            frm.Show();
        }

        private void cmdocultar_Click(object sender, EventArgs e)
        {
            lblconfirmar.Visible = false;
            cmdconfirmar.Visible = false;
            cmdocultar.Visible = false;

            lblinfo_suc.Visible = false;
        }

        private void cmdvoltar_Click(object sender, EventArgs e)
        {
            this.Close();
            frm_padmin frm1 = new frm_padmin();
            frm1.Show();
        }
    }
}
